import 'package:flutter/material.dart';
import 'package:ljy_flutter/provider/provider_counter.dart';
import 'package:ljy_flutter/provider/provider_next_page.dart';
import 'package:provider/provider.dart';

class ProviderApp extends StatelessWidget {
  const ProviderApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(home: ProviderHome());
  }
}

class ProviderHome extends StatelessWidget {
  const ProviderHome({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('provider state control')),
      body: Center(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Consumer<DataProvider>(
              builder: (cox, dp, child) {
                if (dp.isLoading) {
                  return CircularProgressIndicator();
                }
                return Text(dp.data);
              },
            ),
            Text('You have pushed the button this many times:'),
            ProviderCountWidget(),
            SizedBox(height: 20),
            Text('other data'),
            Consumer<ProviderName>(
              builder: (context, pn, child) {
                return Center(
                  child: Container(
                    width: 100,
                    height: 100,
                    child: Text('se count: ${pn.count}'),
                  ),
                );
              },
            ),
            Consumer<ProviderName>(
              builder: (context, pn, child) {
                return IconButton(
                  onPressed: () {
                    pn.increment();
                  },
                  icon: Icon(Icons.add),
                );
              },
            ),

            OutlinedButton(
              onPressed: () {
                Navigator.push(
                  context,
                  MaterialPageRoute(builder: (context) => ProviderNextPage()),
                );
              },
              child: Text('下一个'),
            ),

            Consumer<ProviderName>(
              builder: (context, pn, child) {
                return Text('上个页面的名称：${pn.name}');
              },
            ),

            ElevatedButton(
              onPressed: () {
                context.read<ProviderName>().updateName('新名称是---}');
              },
              child: Text('改变名称'),
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        key: Key('increment_floatingActionButton'),
        tooltip: 'Increment',
        child: Icon(Icons.add),
        onPressed: () {
          context.read<ProviderCounter>().increment();
        },
      ),
    );
  }
}

class ProviderCountWidget extends StatelessWidget {
  const ProviderCountWidget({super.key});

  @override
  Widget build(BuildContext context) {
    return Text(
      '${context.watch<ProviderCounter>().count}',
      key: Key('counterState'),
      style: TextStyle(fontSize: 20),
    );
  }
}
